using System;
using System.Collections.Generic;
using System.Text;
using TTDotNet.Util;
using TTDotNet.Engine;
using TTDotNet.Engine.ThreeDEngine;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using TTDotNet.Engine.Companies;
using TTDotNet.Engine.Industries;
using TTDotNet.Engine.Exceptions;


namespace TTDotNet.Engine.Stations
{
    public class BusStation : Station
    {
        public BusStation(Company company, string name, Coordinate coord, RotateType rotType) 
            : base(name, company,coord, rotType, StationType.Bus_Station)
        {
        
        }

        protected override void SetupAreaCoverage(Coordinate coord)
        {
            catchmentArea = 3;
            List<Coordinate> area = new List<Coordinate>();
            area.Add(coord);
            SetBuildingArea(area);
        }

        protected override void FindIndustries()
        {
            Coordinate temp = new Coordinate();

            foreach (Coordinate coord in areaCovered)
            {
                for(int z = coord.Z - catchmentArea; z <= coord.Z + catchmentArea; z++) {
                    for (int x = coord.X - catchmentArea; x <= coord.X + catchmentArea; x++)
                    {
                        temp.X = x;
                        temp.Z = z;

                        try
                        {
                            Industry ind = Game.gameWorld.Map.GetIndustryOnTile(x, z);
                            if (ind != null)
                            {
                                bool val = ind.AddStationForDelivery(this);
                                if (val)
                                {
                                    //industriesInCatchment.Add(ind);
                                    foreach (CargoType type in ind.Info.Produces)
                                    {
                                        if (!industriesInCatchment.ContainsKey(type))
                                        {
                                            industriesInCatchment[type] = new List<Industry>();
                                        }


                                        industriesInCatchment[type].Add(ind);

                                        cargoAtStation[type] = new Dictionary<Industry, float>();
                                    }
                                    foreach (CargoType type in ind.Info.Accepts)
                                    {
                                        if (!industriesInCatchment.ContainsKey(type))
                                        {
                                            industriesInCatchment[type] = new List<Industry>();
                                        }
                                            
                                        industriesInCatchment[type].Add(ind);
                                        
                                        Accepts.Add(type);
                                        cargoAtStation[type] = new Dictionary<Industry, float>();
                                    }
                                }
                            }
                        }
                        catch (CoordinateOutOfBoundsException)
                        {
                            
                        }

                    }
                }
            }
        }

    }
}